﻿package classes 
{
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import com.ourbrander.loader.EasyLoader
	import com.ourbrander.loader.EasyLoaderEvent
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.Video;
	import flash.net.NetStream;
	
	/**
	 * ...
	 * @author liuyi  email:luckliuyi@163.com, blog:http://www.ourbrander.com; 
	 * This is a very sample example for easyLoader Class.But it's enough to use!
	 * 一个简单的实例，如何使用easyLoader，会这么多就够用了哦！如果需要了解更多高级功能可以查看API，也可以与我联系。
	 */
	public class easyLoader_example extends MovieClip
	{
		private var _assetsManager:EasyLoader
		 
		
		public function easyLoader_example() 
		{
			init()
		}
		private function init() {
		    start_btn.addEventListener(MouseEvent.MOUSE_DOWN,startLoad)
		    add_btn.addEventListener(MouseEvent.MOUSE_DOWN,addAssets)
			initAssets()
		}
		private function initAssets() {
			_assetsManager = new EasyLoader();
			_assetsManager.addEventListener(EasyLoaderEvent.COMPLETED,assetsLoaded)
			_assetsManager.addEventListener(EasyLoaderEvent.LOADING_ERROR,assetsLoadError)
			_assetsManager.addEventListener(EasyLoaderEvent.PROGRESS,assetsLoadProgress)
			_assetsManager.loadConfig("assets.xml")
			_assetsManager.autoLoad = false

			_assetsManager.addType("text",".txt2")//增加自定义的文件类型，add a custom file type.
			_assetsManager.addFile("assets/images/d8.png", "myImg")
			
			
		}
		private function startLoad(e:MouseEvent) {
			_assetsManager.start()
		}
		private function assetsLoaded(e:EasyLoaderEvent) {
			trace("assetsLoaded:\n", e,_assetsManager.index+"/"+_assetsManager.length)
			info_txt.text+=("assetsLoaded:\n"+e+"\nindex:"+_assetsManager.index+"/length:"+_assetsManager.length)
			
		}
		private function assetsLoadError(e:EasyLoaderEvent) {
			trace("assetsLoadError:\n",e)
			info_txt.text += ("assetsLoadError:\n" + e+"\n")
			info_txt.text +=("loadAssets ioErrorHandler->"+"\n")
			info_txt.text+= ("index:"+_assetsManager.index)
			info_txt.text+= ("e.data:"+e.data+"\n")
			for (var i in e.data) {
				info_txt.text +=(i + "=>" + e.data[i]+"\n");
			}
			trace(_assetsManager.ignoreError)
			
		}
		private function assetsLoadProgress(e:EasyLoaderEvent) {
			var p= Math.round((_assetsManager.currentLoadingObj.bytesLoaded/_assetsManager.currentLoadingObj.bytesTotal)*100)
			info_txt.text+="Loaidng Assets Item "+(_assetsManager.index+1)+" Percent:"+p+"%"+"\n"
		}
		
		private function addAssets(e:MouseEvent) {
			var bitmap:Bitmap = _assetsManager.getFileByAlias("myImg").content
			bitmap.y = info_txt.y + info_txt.height
			addChild(bitmap)
			
			
			//below it's a wrong example of loading assets,the path is wrong in config.xml.
			//下面是一个错误的文件地址，这里会抛出错误但是下载会继续，因为默认是忽略下载错误的。
			try{
				var myswf = _assetsManager.getFileByAlias("myswf").content
				myswf.y = bitmap.y + bitmap.height + 10;
				addChild(myswf)
			}catch (e) { trace("loading error:" + e) }
			
			
			//获取加载进来文件,get file from easyLoader 
			var xml:XML = _assetsManager.getFileByAlias("adobe_rss").content
			info_txt.text += "\n\n\nxml======>"+xml.toString()
			
			var txt:String = _assetsManager.getFileByAlias("artical").content
			info_txt.text += "\n\n\ntxt=====>" +txt
			
			var video:Video = new Video()
			var _netstream:NetStream=_assetsManager.getFileByAlias("ckfree").content
			video.attachNetStream(_netstream)
			video.y=bitmap.y+bitmap.height
			_netstream.resume()
			addChild(video)
			
			var mp3:Sound = _assetsManager.getFileByAlias("mymp3").content
			var chanel:SoundChannel = mp3.play(0,9999999)
			
			
			
			
			trace(">>>>>>>>>>"+_assetsManager.getFileByIndex(1))
			//the same way to add dae files to stage,if we have a 3d world!Guys,We’re counting on you this time!
			//dae 文件的获取方法也是这样的，具体的实现就麻烦大家自己练习下吧。不想在这里加入过多文件。
			
			
			
			
		}
		
	}

}